package cn.wolfcode.service.impl;

import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.QuestionTypes;
import cn.wolfcode.mapper.ExamQuestionMapper;
import cn.wolfcode.mapper.QuestionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ExamQuestionServiceImpl implements IExamQuestionService {

    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void save(ExamQuestion examQuestion) {
        examQuestionMapper.insert(examQuestion);
    }



    @Override
    public List<ExamQuestion> listAll() {
        return examQuestionMapper.selectAll();
    }

    @Override
    public void saveByQuestionTypes(QuestionTypes questionTypes, Long id) {
        //获取所有的单选题标题
        List<Long> radios = questionMapper.listAllRadio();
        //打乱单选集合中的顺序
        Collections.shuffle(radios);
        //循环在中间表中插入元素
        for (int i = 0; i < questionTypes.getRadio(); i++) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setExamId(id);
            examQuestion.setQuestionId(radios.get(i));
            examQuestion.setSequence(i+1);
            examQuestion.setScore(questionTypes.getRadioScore());
            examQuestionMapper.insert(examQuestion);
        }

        //获取所有的单选题标题
        List<Long> checkbox = questionMapper.listAllCheckbox();
        //打乱单选集合中的顺序
        Collections.shuffle(checkbox);
        //循环在中间表中插入元素
        for (int i = 0; i < questionTypes.getCheckbox(); i++) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setExamId(id);
            examQuestion.setQuestionId(checkbox.get(i));
            examQuestion.setSequence(i+1);
            examQuestion.setScore(questionTypes.getCheckboxScore());
            examQuestionMapper.insert(examQuestion);
        }

        //获取所有的单选题标题
        List<Long> estimate = questionMapper.listAllEstimate();
        //打乱单选集合中的顺序
        Collections.shuffle(estimate);
        //循环在中间表中插入元素
        for (int i = 0; i < questionTypes.getEstimate(); i++) {
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setExamId(id);
            examQuestion.setQuestionId(estimate.get(i));
            examQuestion.setSequence(i+1);
            examQuestion.setScore(questionTypes.getEstimateScore());
            examQuestionMapper.insert(examQuestion);
        }
    }

}
